import { startActivityList,joinActivityList,deleteActivity } from '@/util/api/activity.js'
export default{
  data() {
    return {
      tabList: [
        {
          name: '创建的',
          count: 0,
        },
        {
          name: '参加的',
          count: 0,
        },
      ],
      current: 0,
      total: 0,
      list: [],
      params: {
        pageNo: 1,
        pageSize: 15,
      },
    }
  },
  onShow() {
    this.fetchData(true)
  },
  methods: {
    toCode(item) {
      if(item.status === 1){
        uni.navigateTo({
          url: `/pages/activity/code/Index?id=${item.id}`,
        })
      }else{
        uni.showToast({
          title: `活动${item.status===2?'未开始':'已结束'}`,
          duration: 2000,
          icon: "none"
        });
      }
    },
    createActive() {
      let token = this.$getToken();
      if (!token) {
        uni.showModal({
          title: '提示',
          content: '您还没有登录，请登录后操作！',
          cancelText: '取消', // 取消按钮的文字
          confirmText: '去登录', // 确认按钮文字
          confirmColor: '#1678ff',
          success: res => {
            if (res.confirm) {
              uni.setStorageSync("pageUrl", window.location.href);
              uni.navigateTo({
                url: '/pages/login/index'
              })
            } else {
              
            }
          },
        })
      }else{
        uni.navigateTo({
          url: '/pages/activity/add/Index',
        })
      }
    },
    // tab切换
    handleChange(index) {
      this.current = index
      this.fetchData(true)
    },
    getMyActivityList() {
      startActivityList(this.params).then(res=>{
        this.list = res.result.records
      })
    },
    fetchData(reset = false) {
      if (reset) {
        this.resetPagination()
      }
      uni.showLoading()
      this.getPromise().then(res => {
        uni.hideLoading()
        this.$nextTick(() => {
          let records = res.result.records
          this.tabList[this.current]['count'] = res.result.total
          this.total = res.result.total
          this.list = reset ? records : this.list.concat(records)
          this.params.pageNo += 1
        })
      })
    },
    resetPagination() {
      this.params.pageNo = 1
      this.list = []
    },
    getPromise() {
      switch (this.current) {
        case 0:
          return startActivityList(this.params)
        case 1:
          return joinActivityList(this.params)
      }
    },
    toDetail(item) {
      uni.navigateTo({
        url: `/pages/activity/detail/Index?id=${item.id}`,
      })
    },
    toPerson(item) {
      uni.navigateTo({
        url: `/pages/activity/personList/Index?id=${item.id}`,
      })
    },
    toDelete(item) {
      uni.showModal({
        title: '提示',
        content: '确认删除?',
        cancelText: "取消", // 取消按钮的文字 
        confirmText: "确认", // 确认按钮文字 
        confirmColor: '#1678ff',
        success: (res) => {
          if(res.confirm) {
            deleteActivity(item.id).then((res) => {
              if (res.code === 200) {
                uni.showToast({
                    title: "操作成功",
                    duration: 2000,
                    icon: "none",
                });
                setTimeout(() => {
                  this.fetchData(true)
                }, 1000)
              } else {
                uni.showToast({
                    title: res.message,
                    duration: 2000,
                    icon: "none",
                });
              }
            })
          }
        }
      })
    },
    toEdit(item) {
      uni.navigateTo({
        url: `/pages/activity/add/Index?type=2&id=${item.id}`,
      })
    }
  }
}